<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

/**
 * Facilitate the collection of a date with dropmenus
 * @author Anthony Gallon
 * @package Antz
 */


/**
 * Facilitate the collection of a date with dropmenus
 * @author Anthony Gallon
 * @package Antz
 */
class JFX_Form_Components_DateSelect extends JFX_Form_Component
{
    /**
     * Contains the options
     * @var array $options
     */
    protected $options = array();

    /**
     * Define the type for auto-id
     * @var string $type
     */
    protected $type = 'DateSelect';

    /**
     * Set some initial values
     * @param string $name
     * @param string $label
     * @param string $value
     * @param int $yearStart
     * @param int $yearEnd
     */
    public function __construct($name, $label, $value='', $yearStart='', $yearEnd=''){
        $this->id = 'sel'.rand();
        $this->name = $name;
        $this->label = $label;
        $this->yearStart = $yearStart;
        $this->yearEnd = $yearEnd;

        if($this->yearStart=='') $this->yearStart = date('Y', time()-86400*90);
        if($this->yearEnd=='') $this->yearEnd = date('Y', time()+86400*90);

        if($value=='') $value = array('month'=>date('n'), 'year'=>date('Y'), 'day'=>date('j'));

        $this->value = $value;
        $this->setLang('en');
    }

    /**
     * Set which language to use, determining the month titles
     * @param string $lang
     */
    public function setLang($lang='en'){
        $lang = 'en';
        switch(strtolower($lang)){
            case 'en':
                $this->months = array(
                    '1'=>'January',
                    '2'=>'February',
                    '3'=>'March',
                    '4'=>'April',
                    '5'=>'May',
                    '6'=>'June',
                    '7'=>'July',
                    '8'=>'August',
                    '9'=>'September',
                    '10'=>'October',
                    '11'=>'November',
                    '12'=>'December'
                );
            break;
        }
    }

    /**
     * Return the object as an HTML string
     * @global object $gCms
     * @param bool $wrap
     * @param bool $label
     * @return string $html
     */
    public function render($wrap=true, $label=true){
        // generate the month options
        $this->monthSel = new JFX_Form_Components_DateSelect_Select($this->name.'Month', $this->value['month']);

        for($i=1; $i<=12; $i++){
            $this->monthSel->add($this->months[$i], $i);
        };

        // generate the day options
        $this->daySel = new JFX_Form_Components_DateSelect_Select($this->name.'Day', $this->value['day']);
        for($i=1; $i<=31; $i++){
            $this->daySel->add($i);
        };
        // generate the year options
        $this->yearSel = new JFX_Form_Components_DateSelect_Select($this->name.'Year', $this->value['year']);
        for($i=$this->yearStart; $i<=$this->yearEnd; $i++){
            $this->yearSel->add($i);
        };

		global $gCms;
        $CONFIG = JFX::registry('config');
     	if($this->required) $this->aftertext = '<span class="required"><img src="'.$CONFIG->baseUrl.'/jfx/images/required.gif" alt="Required" /></span>'.$this->aftertext;


        $returnString = $this->monthSel->render().$this->daySel->render().$this->yearSel->render();
        if($this->showLabel) $returnString = '<label>'.$this->label.'</label>'.$returnString.'<div class="aftertext">'.$this->aftertext.'</div>';
        if($this->wrap) return $this->wrap($returnString);
        else return $returnString;
    }
}
